package com.anorak.betsy;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Music {

    private static final String SEARCH_URL = "http://127.0.0.1:8899/qq/search/";
    public static void main(String[] args) throws IOException {
        String QQPath = "/Users/anorak/Downloads/QQMusic/";
        List<File> fileList = FileUtil.loopFiles(QQPath);
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        Request request = new Request.Builder()
                .url(SEARCH_URL + "p:693837784/1/1")
                .method("GET", null)
                .build();
        Response response = client.newCall(request).execute();
        assert response.body() != null;
        JSONObject json = new JSONObject(response.body().string().trim());
        JSONArray array = json.getJSONArray("list");
        List<String> qq = new ArrayList<>();
        for (Object object : array) {
            JSONObject json1 = new JSONObject(object.toString());
            String name = json1.getStr("title")  ;
            qq.add(name.trim());
        }
        List<String> files = new ArrayList<>();
        for (File file : fileList) {
            if (file.isFile()){
                String n1 = file.getName().trim().replace(".mp3","")
                        .replace(".flac","");
                if ( n1.contains(" - ") ){
                    files.add(n1.split(" - ")[1]);
                }else {
                    files.add(n1);
                }
            }
        }
//        System.out.println(files);
        System.out.println(qq.removeAll(files));
        System.out.println(qq);

    }

}
